package com.panda.controller;/**
 * @author 乐于吕
 * * @date 2024/10/15
 */

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.net.URI;
import java.util.Arrays;
import java.util.List;
import java.util.Random;


/**
 * @Author: 乐于吕
 * @CreateTime: 2024-10-15
 * @Description:
 */

@RestController
@RequestMapping("/student")
public class StudentController {


    //注入spring中封装的可以发送http请求的  对象
//  自定义new一个restTemplate      @LoadBalanced负载均衡
//  注入后可以使用restTemplate.getForObject方法拉取节点发送http请求
    @Autowired
    private RestTemplate restTemplate; //配置后注入
//    根据服务名称到服务注册中心拉取节点
    @Autowired
    private DiscoveryClient discoveryClient; //直接注入
//    loadBalancerClient.choose方法 从服务注册中心拉取一个节点   轮循
    @Autowired
    private LoadBalancerClient loadBalancerClient; //直接注入


    @GetMapping("m1")
    public String method1() {
        //注入调用course服务  course m1   http请求  http://localhost:8540/course/m1 response
        //restTemplate  请求course服务
        //    如何实现http请求发送
        RestTemplate restTemplate = new RestTemplate();
        String info = restTemplate.getForObject("http://" + getRandomHost() + "/course/m1", String.class);
        return "studentController service success invoke courseController -->" + info;
    }

    @GetMapping("m2")
    public String method2() {
        List<ServiceInstance> courseservices = discoveryClient.getInstances("COURSESERVICE");
//        courseservices.forEach(courseservice -> System.out.println(courseservice.getHost() + courseservice.getUri()));
        Random random = new Random();
        int index = random.nextInt(courseservices.size());
        URI uri = courseservices.get(index).getUri();

        RestTemplate restTemplate = new RestTemplate();
        String info = restTemplate.getForObject(uri + "/course/m1", String.class);


        return "studentController service success invoke courseController -->" + info;
    }

    @GetMapping("m3")
    public String method3() {
        ServiceInstance courseservice = loadBalancerClient.choose("COURSESERVICE");
        URI uri = courseservice.getUri();

        RestTemplate restTemplate = new RestTemplate();
        String info = restTemplate.getForObject(uri + "/course/m1", String.class);
        return "studentController service success invoke courseController -->" + info;
    }

    @GetMapping("m4")
    public String method4() {
        String info = restTemplate.getForObject("http://COURSESERVICE/course/m1", String.class);
        return "studentController service success invoke courseController -->" + info;
    }


    //    随机获取主机地址  1、健康状态无法控制  2、服务扩充无法实现
    public String getRandomHost() {
//        主机治理
        List<String> hosts = Arrays.asList("localhost:8540", "localhost:8541", "localhost:8542");
        Random random = new Random();
        int index = random.nextInt(hosts.size());
        return hosts.get(index);
    }

}
